﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class GameManager : MonoBehaviour
{
    public static GameManager Instance { get; private set; }

    public int GameMode; //0 单人 1 双人 2 网络

    public int GameLevel;//0 普通 1 噩梦 2 地狱

    public GameObject ChessBoard_obj;
    public GameObject Gird_obj;//格子的实例
    public GameObject Chess_Obj;//棋子的实例
    public Sprite[] ChessSprites;
 
    protected float GridWidth =63.5f;
    protected float GridHeight=63.5f;

    public int[,] ChessBoard;
    public GameObject[,] boardGrids= new GameObject[10,9];//10行,9列


    // 加载脚本实例时调用 Awake
    private void Awake()
    {
        Instance = this;
      
    }

    // Start is called before the first frame update
    void Start()
    {
       
    }

    // Update is called once per frame
    void Update()
    {
        
    }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="i"></param>
    public void SetGameMode(int i)
    {
        GameMode = i;
        // throw new System.NotImplementedException();
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="level"></param>
    public void SetGameLevel(int level)
    {
        GameLevel = level;
        //  throw new System.NotImplementedException();
    }

    public void ResetGame()
    {
        InitGrid();
        InitChess();

        // throw new System.NotImplementedException();
    }
   

 
    /// <summary>
    /// 实例化格子
    /// </summary>
    public void InitGrid()
    {
        float posX=0, posY = 0;
        for (int y = 0; y <= 9; y++)
        {
            for (int x = 0; x <= 8; x++)
            {
                GameObject grid_obj_tmp = Instantiate(Gird_obj);
                grid_obj_tmp.transform.SetParent(ChessBoard_obj.transform);
                grid_obj_tmp.transform.localPosition=new Vector3(posX,posY,0);
                grid_obj_tmp.name = $"{x}_{y}";
                posX += GridWidth;
                if (posX>GridWidth*8)
                {
                    posX = 0;
                    posY += GridHeight;
                }

                grid_obj_tmp.GetComponent<ChessOrGrid>().Xindex = x;
                grid_obj_tmp.GetComponent<ChessOrGrid>().Yindex = y;
                boardGrids[y, x] = grid_obj_tmp;


            }
        }

    }
    /// <summary>
    /// 实例化棋子
    /// </summary>
    private void InitChess()
    {
        ChessBoard = new int[,]
        {
            {2, 3, 6, 5, 1, 5, 6, 3, 2},
            {0, 0, 0, 0, 0, 0, 0, 0, 0},
            {0, 4, 0, 0, 0, 0, 0, 4, 0},
            {7, 0, 7, 0, 7, 0, 7, 0, 7},
            {0, 0, 0, 0, 0, 0, 0, 0, 0},
            {0, 0, 0, 0, 0, 0, 0, 0, 0},
            {14, 0, 14, 0, 14, 0, 14, 0, 14},
            {0, 11, 0, 0, 0, 0, 0, 11, 0},
            {0, 0, 0, 0, 0, 0, 0, 0, 0},
            {9, 10, 13, 12, 8, 12, 13, 10, 9}
        };
        for (int y = 0; y < 10; y++)
        {
            for (int x = 0; x < 9; x++)
            {
                var grid_obj_temp = boardGrids[y, x];
                switch (ChessBoard[y, x])
                {
                    case 1:
                        createChess(grid_obj_temp, "heijiang", ChessSprites[0], false);
                        break;
                    case 2:
                        createChess(grid_obj_temp, "heiche", ChessSprites[1], false);
                        break;
                    case 3:
                        createChess(grid_obj_temp, "heima", ChessSprites[2], false);
                        break;
                    case 4:
                        createChess(grid_obj_temp, "heipao", ChessSprites[3], false);
                        break;
                    case 5:
                        createChess(grid_obj_temp, "heixiang", ChessSprites[4], false);
                        break;
                    case 6:
                        createChess(grid_obj_temp, "heishi", ChessSprites[5], false);
                        break;
                    case 7:
                        createChess(grid_obj_temp, "heibing", ChessSprites[6], false);
                        break;
                    case 8:
                        createChess(grid_obj_temp, "hongjiang", ChessSprites[7], true);
                        break;
                    case 9:
                        createChess(grid_obj_temp, "hongche", ChessSprites[8], true);
                        break;
                    case 10:
                        createChess(grid_obj_temp, "hongma", ChessSprites[9], true);
                        break;
                    case 11:
                        createChess(grid_obj_temp, "hongpao", ChessSprites[10], true);
                        break;
                    case 12:
                        createChess(grid_obj_temp, "hongxiang", ChessSprites[11], true);
                        break;
                    case 13:
                        createChess(grid_obj_temp, "hongshi", ChessSprites[12], true);
                        break;
                    case 14:
                        createChess(grid_obj_temp, "hongbing", ChessSprites[13], true);
                        break;
                }
            }
        }
    }
   /// <summary>
   /// 创建棋子
   /// </summary>
   /// <param name="grid_obj"></param>
   /// <param name="name"></param>
   /// <param name="chessSprite"></param>
   /// <param name="isred"></param>
    private void createChess(GameObject grid_obj, string name, Sprite chessSprite, bool isred)
    {
        GameObject chess_obj = Instantiate(Chess_Obj);
        chess_obj.name = name;
        chess_obj.GetComponent<Image>().sprite = chessSprite;
        chess_obj.transform.SetParent(grid_obj.transform);
        chess_obj.transform.localPosition = Vector3.zero;
        chess_obj.transform.localScale = Vector3.one;
        chess_obj.GetComponent<ChessOrGrid>().isred = isred;
    }
}
